﻿using System;
using System.Collections.Generic;
using System.Text;
using Tao.OpenGl;
using PacMan3D.Utils;
using System.IO;
using Tao.DevIl;
using PacMan3D.Exceptions;
using PacMan3D.Windows;
using PacMan3D.IO;

namespace PacMan3D.Objects
{
    public class Banner : IRenderable
    {
        private int TextureId;
        private int TextureIdMask;
        private int BannerHeight = 40;
        private Maze Maze;

        public Banner(Maze maze)
        {
            this.Maze = maze;
            this.TextureId = ResourceLoader.LoadResource(Constants.RES_TEXT_BANNER);
            this.TextureIdMask = ResourceLoader.LoadResource(Constants.RES_TEXT_BANNER_MASK);
        }

        public override void DrawObject()
        {
            Gl.glViewport(0, Window.Instance().Height - BannerHeight, Window.Instance().Width, BannerHeight);

            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            Glu.gluOrtho2D(0.0f, Window.Instance().Width, 0.0f, BannerHeight);

            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();

            Gl.glDisable(Gl.GL_DEPTH_TEST);
            Gl.glDisable(Gl.GL_CULL_FACE);

            Gl.glEnable(Gl.GL_TEXTURE_2D);
            Gl.glBindTexture(Gl.GL_TEXTURE_2D, this.TextureIdMask);
            Gl.glTexEnvf(Gl.GL_TEXTURE_ENV, Gl.GL_TEXTURE_ENV_MODE, Gl.GL_DECAL);
            Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
            
            Gl.glEnable(Gl.GL_BLEND);
            Gl.glBlendFunc(Gl.GL_DST_COLOR, Gl.GL_ZERO);
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex2f(0, 0);
            Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex2f(0, BannerHeight);
            Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex2f(Window.Instance().Width, BannerHeight);
            Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex2f(Window.Instance().Width, 0);
            Gl.glEnd();

            Gl.glBindTexture(Gl.GL_TEXTURE_2D, this.TextureId);
            Gl.glBlendFunc(Gl.GL_ONE, Gl.GL_ONE);
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex2f(0, 0);
            Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex2f(0, BannerHeight);
            Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex2f(Window.Instance().Width, BannerHeight);
            Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex2f(Window.Instance().Width, 0);
            Gl.glEnd();

            Gl.glDisable(Gl.GL_BLEND);
            Gl.glDisable(Gl.GL_TEXTURE_2D);

            int numLifes = ((this.Maze.PacMan.Lifes >= 0) ? this.Maze.PacMan.Lifes : 0);
            Font.DrawNumber(this.Maze.PacMan.Points, new Point3D((0.13125f * Window.Instance().Width), (0.1f * this.BannerHeight)));
            Font.DrawNumber(this.Maze.GetFoodLeft(), new Point3D((0.585f * Window.Instance().Width), (0.1f * this.BannerHeight)));
            Font.DrawNumber(numLifes, new Point3D((0.965f * Window.Instance().Width), (0.1f * this.BannerHeight)));

          
            Gl.glEnable(Gl.GL_DEPTH_TEST);
            Gl.glEnable(Gl.GL_CULL_FACE);
        }

        /// <summary>
        /// Método que desenha um quadrado vermelho a representar o banner.
        /// </summary>
        private void DrawSquare()
        {
            Gl.glColor3f(1.0f, 0.0f, 0.0f);
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glVertex2f(0.0f, 0.0f);
            Gl.glVertex2f(0.0f, BannerHeight);
            Gl.glVertex2f(Window.Instance().Width, BannerHeight);
            Gl.glVertex2f(Window.Instance().Width, 0.0f);
            Gl.glEnd();
        }
    }
}
